package com.example.qixin0314.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.qixin0314.R;
import com.example.qixin0314.bean.ObjectAdapter;
import com.example.qixin0314.bean.ObjectModel;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import cn.leancloud.LCObject;
import cn.leancloud.LCQuery;
import cn.leancloud.LCUser;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class followFragment extends Fragment {
    private static final String TAG = followFragment.class.getSimpleName();
    RecyclerView recycle_gz;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_follow, container, false);
        initview(root);
        //Arecycle_gz();
        return root;

    }

    private void initview(View root) {
        recycle_gz= root.findViewById(R.id.recycle_gz);
        LinearLayoutManager llm = new LinearLayoutManager(getContext());
        recycle_gz.setLayoutManager(llm);

    }
    private void Arecycle_gz(){
        List<ObjectModel>companyData=new ArrayList<>();
        ObjectAdapter adapter=new ObjectAdapter(getActivity());
        adapter.setCompanyData(companyData);
        recycle_gz.setAdapter(adapter);
        LCUser currents=LCUser.getCurrentUser();
        if (currents!=null){
            LCQuery<LCObject>query=new LCQuery<>("focus");
            query.whereEqualTo("id",currents.getObjectId());
            query.findInBackground()
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<List<LCObject>>() {
                        @Override
                        public void onSubscribe(@NotNull Disposable d) { }

                        @Override
                        public void onNext(@NotNull List<LCObject> lcObjects) {
                            for (int i = 0; i < lcObjects.size(); i++) {
                                ObjectModel model=new ObjectModel();
                                model.setImageurl(lcObjects.get(i).get("imageurl").toString());
                                model.setTitle(lcObjects.get(i).get("title").toString());
                                model.setUrl(lcObjects.get(i).get("url").toString());
                                companyData.add(model);
                            }
                            adapter.notifyDataSetChanged();
                        }

                        @Override
                        public void onError(@NotNull Throwable e) {

                        }

                        @Override
                        public void onComplete() {

                        }
                    });

        }



    }

    @Override
    public void onResume() {
        Arecycle_gz();
        super.onResume();
    }
}